#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>

pthread_mutex_t lock_1;
pthread_mutex_t lock_2;

void* fun_1(void*);
void* fun_2(void*);

int main(int argc,char** argv)
{
  int ret;
  ret = pthread_mutex_init(&lock_1,NULL);
  if(ret != 0)
  {
    fprintf(stderr,"pthread_mutex_init lock_1 is fail\n");
    exit(-1);
  }

  ret = pthread_mutex_init(&lock_1,NULL);
  if(ret != 0)
  {
    fprintf(stderr,"pthread_mutex_init lock_2 is fail\n");
    exit(-1);
  }
  
  pthread_t tid;
  pthread_attr_t attr;
  ret = pthread_attr_init(&attr);
  if(ret != 0)
  {
    fprintf(stderr,"pthread_mutex_attr is fail\n");
    exit(-1);
  }

  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
  pthread_create(&tid,&attr,fun_1,NULL);
  pthread_create(&tid,&attr,fun_2,NULL);

  pthread_attr_destroy(&attr);
  pthread_exit((void*)0);
}

void* fun_1(void* n)
{
  int ret;
  while(1)
  {
    ret = pthread_mutex_trylock(&lock_1);
    if(ret == 0)
    {
      ret = pthread_mutex_trylock(&lock_2);
      if(ret == 0)
      {
        printf("I find \n");
      }
    }
  }
  sleep(1);
}
void* fun_2(void* num)
{
  int ret;
  while(1)
  {
    ret = pthread_mutex_trylock(&lock_2);
    if(ret == 0)
    {
      ret = pthread_mutex_trylock(&lock_1);
      if(ret == 0)
      {
        printf("I find \n");
      }
    }
  }
  sleep(1);

}
